//! 字符串处理工具

/// 检查字符串是否为回文
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::string_utils::is_palindrome;
/// 
/// assert_eq!(is_palindrome("level"), true);
/// assert_eq!(is_palindrome("hello"), false);
/// ```
pub fn is_palindrome(s: &str) -> bool {
    let cleaned: String = s.chars().filter(|c| c.is_alphanumeric()).collect();
    let lowercased = cleaned.to_lowercase();
    lowercased == lowercased.chars().rev().collect::<String>()
}

/// 反转字符串
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::string_utils::reverse_string;
/// 
/// assert_eq!(reverse_string("hello"), "olleh");
/// ```
pub fn reverse_string(s: &str) -> String {
    s.chars().rev().collect()
}

/// 统计字符串中某个字符出现次数
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::string_utils::count_char;
/// 
/// assert_eq!(count_char("hello world", 'l'), 3);
/// ```
pub fn count_char(s: &str, c: char) -> usize {
    s.chars().filter(|&ch| ch == c).count()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_palindrome() {
        assert_eq!(is_palindrome("level"), true);
        assert_eq!(is_palindrome("A man a plan a canal Panama"), true);
        assert_eq!(is_palindrome("hello"), false);
    }

    #[test]
    fn test_reverse_string() {
        assert_eq!(reverse_string("hello"), "olleh");
        assert_eq!(reverse_string(""), "");
        assert_eq!(reverse_string("a"), "a");
    }

    #[test]
    fn test_count_char() {
        assert_eq!(count_char("hello world", 'l'), 3);
        assert_eq!(count_char("hello world", 'o'), 2);
        assert_eq!(count_char("hello world", 'x'), 0);
    }
}